<template>
  <el-form
    status-icon
    ref="userInfoForm"
    :rules="userInforules"
    :model="userInfoForm"
    label-width="100px"
  >
    <el-form-item
      label="原密码"
      prop="password"
      style="width:300px;"
    >
      <el-input
        type="password"
        v-model="userInfoForm.password"
        placeholder="请输入原密码"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="新密码"
      prop="newPassword"
      style="width:300px;"
    >
      <el-input
        type="password"
        v-model="userInfoForm.newPassword"
        autocomplete="off"
        placeholder="请设置新密码"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="重复新密码"
      prop="checkNewPassword"
      style="width:300px;"
    >
      <el-input
        type="password"
        v-model="userInfoForm.checkNewPassword"
        autocomplete="off"
        placeholder="请确认新密码"
        @keyup.enter.native="submitForm('userInfoForm')"
      ></el-input>
    </el-form-item>
    <div style="margin-left:auto;margin-right:auto;width:20%;">
      <el-button
        type="primary"
        @click="submitForm('userInfoForm')"
      >提交</el-button>
      <el-button @click="resetForm('userInfoForm')">重置</el-button>
    </div>
  </el-form>
</template>

<script>
import { request } from 'network/request'
export default {
  name: 'SetPassword',
  data () {
    var validateNewPass = (rule, value, callback) => {
      if (value === this.userInfoForm.password) {
        callback(new Error('新密码与原密码相同!')
        )
      } else if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{8,}$/.test(value)) {
        callback(new Error('需包含大小写字母及数字，8位以上'))
      } else {
        if (this.userInfoForm.checkNewPassword !== '') {
          this.$refs.userInfoForm.validateField('checkNewPassword')
        }
        callback()
      }
    }
    var validateNewPass2 = (rule, value, callback) => {
      if (value !== this.userInfoForm.newPassword) {
        callback(new Error('与新密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userInfoForm: {
        username: null,
        email: null,
        password: null,
        newPassword: null,
        checkNewPassword: null
      },
      userInforules: {
        newPassword: [
          { validator: validateNewPass, trigger: ['blur'] }
        ],
        checkNewPassword: [
          { validator: validateNewPass2, trigger: ['blur'] }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          request({
            url: '/user',
            method: 'put',
            data: this.userInfoForm
          }).then(res => {
            // console.log(response.data);
            // console.log(response.status);
            this.$message({
              message: '密码更改成功',
              type: 'success',
              duration: 1000
            })
            this.$refs.userInfoForm.resetFields()
          }).catch(err => {
            if (err) {
              this.$message({
                message: '密码更改失败',
                type: 'error',
                duration: 1000
              })
            }
          })
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
